#include "game_2.h"
#include "Allegro_initialize.h"

Game::Game (vector<vector<int>> &_level_data)
{
	init_game(_level_data);
}

Game::Game ()
{
	init_game();
}

void Game::init_game()
{
	_dig_dug = Digdug(_game_grid_elements/2 ,_game_grid_elements/2,0,1,1);
//	_monster = Monster;
	_screen_map = generate_default_map();
}

void Game::init_game(vector<vector<int>> &_level_data)
{
	//
	//Level Data contains our objects, stored 
	//The first thign we want to do is generate our objects from the provided data
	//to populate our map from our
	//Preloaded map data.
	
	

	//Now we want to create our movable objects
	
}

void Game::update(Direction dir)
{
	//If keyboard input != shoot
	//Change the direction of digdug
	//then call querymove and pass in digdug
	//
	if(dir !=NONE)
	{
	move_object(_dig_dug, dir);
	}

}


vector<vector<Movable_type>> Game::generate_default_map()
{
	vector<Movable_type> _line_map;
	vector<vector<Movable_type>> _lines_map;
	for(int i =0; i < _game_grid_elements; i++)
	{
		_line_map.clear();

		for(int j = 0; j < _game_grid_elements; j++)
		{
			if((i>=0.3*_game_grid_elements) && (i<0.6*_game_grid_elements) && (j>=0.3*_game_grid_elements) && (j<0.6*_game_grid_elements))
			{
				_line_map.push_back(HOLE);
			}
			else
			{
				_line_map.push_back(SAND);
			}

		}
		_lines_map.push_back(_line_map);
	}

	return _lines_map;
}

 bool Game::is_sand_at (int i, int j)
 {
	 if(_screen_map[i][j] == SAND)
	 {
		 return true;
	 }
	 return false;
 }

 int Game::get_number_of_objects()
 {
	 return 1;
 }

int Game::get_object_x(int object_count)
{
	return _dig_dug.get_x_location();
}
int Game::get_object_y(int object_count)
{
	return _dig_dug.get_y_location();
}
int Game::get_object_border(int object_count)
{
	return _dig_dug.get_x_boundary();
}
ALLEGRO_BITMAP * Game::get_object_bitmap(int object_count)
{
	return _dig_dug.get_bitmap();
}

Direction Game::get_object_direction (int object_count)
{
	return _dig_dug.get_direction();
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
Rules Game::move_object(Digdug &movable, Direction dir)
{
	//Object Variables
	int _x_bound = movable.get_x_boundary();
	int _y_bound = movable.get_y_boundary();
	int _x_pos = movable.get_x_location();
	int _y_pos = movable.get_y_location();
	int _speed = movable.get_speed();

	//Movement variables
	int _x = 0; 
	int _y = 0;
	int _dx = 0;
	int _dy = 0;
	int _x_old = 0;
	int _y_old = 0;
	Rules _move_result = DECIDING;

	//Backup variables
	vector<vector<Movable_type>> _screen_map_backup;

	//Name shortening using reference
	int _grid = _game_grid_elements;

	//Set the change in direction
	switch(dir)
	{
	case UP:	_dy = -1;	break;
	case DOWN:	_dy = 1;	break;
	case RIGHT:	_dx = 1;	break;
	case LEFT:	_dx = -1;	break;
	}

	//Each move in a given direction is peformed one step at a time. 
	//ie. For a speed of one, it moves once, a speed of two it moves twice. 
	for(int loop = 0; loop < _speed && _move_result == DECIDING ; loop++)
	{
		//Create a tempory backup of our current screen map and location
		//This will be used to restore any changes made if the move is then 
		//determined to be invalid
		_screen_map_backup = _screen_map;
		_x_old = movable.get_x_location();
		_y_old = movable.get_y_location();
		
		//Now going through all of our located grid elements, 
		//Check what the move will result in And then move to the position if possible
		for(int i = -_x_bound; i <= _x_bound &&_move_result == DECIDING; i++)
		{
			for (int j = -_y_bound; j <= _y_bound  && _move_result == DECIDING; j++)
			{
				//The vector position on the grid that we will be checking
				int _x_new = _x_old + i +_dx;
				int _y_new = _y_old + j + _dy;

				//Making sure it is within bounds at all times, if not the move is not allowed.
				if( (_x_new >= 0) && (_x_new <= _grid -_x_bound +1) && (_y_new >= 0) && (_y_new <= _grid - _y_bound +1) )
				{
					//Run through the RULE LIST for that object
					//for(int r = 0; r < movable.get_rule_set().size(); r++)
					for(int r = 0; r < 1; r++)
					{
						//Cant to move that part of movable to that location
						if(_screen_map[_x][_y] !=  movable.get_rule_set()[CAN_MOVE_THROUGH][r])
						//if(_screen_map[_x][_y] != SAND && _screen_map[_x][_y] != HOLE) 
						{
							_move_result = CANT_MOVE_THROUGH;
							//_move_result = DECIDING;
						}
						//A move request to that location results in the death of DIGDUG
						//else if(_screen_map[_x][_y] ==  movable.get_rule_set()[DEATH_TO_DIGDUG][r])
						else if(_screen_map[_x][_y] ==  MONSTER)
						{
							_move_result = DEATH_TO_DIGDUG;
						}

						//A move request to that location results in a death of the object itself
						//else if(_screen_map[_x][_y] !=  movable.get_rule_set()[DEATH_TO_ME][r])
						/*else if(_screen_map[_x][_y] ==  
						{
							_move_result = DEATH_TO_ME;
						}*/
						else
						{
							_screen_map[_x_new-_dx][_y_new-_dy] = HOLE;
							//_screen_map[_x_old][_y_old] = movable.get_movable_type();
							_screen_map[_x_old][_y_old] = DIGDUG;
							movable.set_location(_x_old +_dx,_y_old+_dy);
						}
						
					}
				}
				else
				{
					_move_result = OUT_OF_BOUNDS;
				}
			}
		}
			//The loop has run for a single move in a single direction
			//Now based on the result of the move, an action will be carried out
			if(_move_result != DECIDING)
			{
				//switch(_move_result)
				//{
				//case A: ; break;
				//case B: ; break;
				//case C: ; break;
				//case D: ; break;
				//case E: ; break;
				//}
				
				_screen_map = _screen_map_backup;
				movable.set_location(_x_old,_y_old);
			}
	}
	return _move_result;
}